package com.njh.kaleidoscope.common.file;

import android.os.Environment;
import android.os.StatFs;

import java.io.File;

/**
 * 存储工具类
 */
@SuppressWarnings("deprecation")
public class SDUtils {
    private static SDUtils mSDUtils;

    public static SDUtils getInstance() {
        return mSDUtils == null ? mSDUtils = new SDUtils() : mSDUtils;
    }

    public static final long BYTE = 1;                            //byte
    public static final long KB = 1024 * BYTE;                    //km
    public static final long MB = 1024 * KB;                    //mb
    public static final long CARD_MIN_CACHE_SIZE = 10 * MB;        //SD卡最小缓存10m

    /**
     * 手机内存的可用空间大小
     *
     * @return
     */

    public long getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }

    /**
     * 手机内存的总空间大小
     *
     * @return
     */
    public long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    }

    /**
     * sd卡的可用空间大小
     *
     * @return
     */
    public long getAvailableExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return availableBlocks * blockSize;
        } else {
            return 0;
        }
    }

    /**
     * sd卡可用空间大小是否足够
     *
     * @return
     */
    public boolean hasFreeSpace() {
        return getAvailableExternalMemorySize() > CARD_MIN_CACHE_SIZE;
    }

    /**
     * sd卡的总空间大小
     *
     * @return
     */
    public long getTotalExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            return totalBlocks * blockSize;
        } else {
            return 0;
        }
    }

    /**
     * sd卡是否可用
     *
     * @return
     */
    public boolean externalMemoryAvailable() {
        String currentState = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(currentState);
    }

    /**
     * 获取SDCard存储根目录
     */
    public String externalMemoryRootPath() {
        if (externalMemoryAvailable()) {
            return Environment.getExternalStorageDirectory().getPath();
        }
        return "/";
    }


    /**
     * 获取手机内部存储空间
     */
    public String internalMemoryRootPath() {
        return Environment.getDataDirectory().getPath();
    }
}
